package com.bree.erp.jsf.view;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import org.apache.log4j.Logger;
import org.primefaces.event.SelectEvent;
import org.primefaces.model.LazyDataModel;

import com.bree.erp.entity.MasterItem;
import com.bree.erp.entity.PurchaseRequisition;
import com.bree.erp.entity.Status;
import com.bree.erp.entity.Unit;
import com.bree.erp.jsf.datatable.LazyBreeDataModel;
import com.bree.erp.service.ItemRequestService;
import com.bree.erp.service.MasterItemService;
import com.bree.erp.service.PurchaseRequisitionService;
import com.bree.erp.service.UnitService;

@ManagedBean(name="dtMasterItemLazyView")
@ViewScoped
@Deprecated
public class MasterItemLazyView {

	private static Logger LOG = Logger.getLogger(MasterItemLazyView.class);

    private LazyDataModel<MasterItem> lazyModel;

    private MasterItem selectedItem;
    
    private String unitCode;
    private double quantity;
    private String remarks;
    
    @EJB
    private MasterItemService miService;
    
    @EJB
    private ItemRequestService irService;
    
    @EJB
    private PurchaseRequisitionService prService;
    
    @EJB
    private UnitService unitService;
    
    @PostConstruct
    public void init() {
    	lazyModel = new LazyBreeDataModel<MasterItem, MasterItemService, Long>(miService, 0L);
    }

	public LazyDataModel<MasterItem> getLazyModel() {
				
        return lazyModel;
    }
 
    public MasterItem getSelectedItem() {
        return selectedItem;
    }
 
    public void setSelectedItem(MasterItem selectedItem) {
        this.selectedItem = selectedItem;
    }
     
    public void onRowSelect(SelectEvent event) {
        FacesMessage msg = new FacesMessage("Item Selected", ((MasterItem) event.getObject()).getId() + "");
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }
	
	private PurchaseRequisition getSessionPurchaseRequisition() {
		FacesContext context = FacesContext.getCurrentInstance();

		PurchaseRequisition session = (PurchaseRequisition) context.getApplication().evaluateExpressionGet(context, "#{purchaseRequisitionBean}", PurchaseRequisition.class);
		
		return session;
	}
    
    public void addItem() {
    	
    	PurchaseRequisition pr = getSessionPurchaseRequisition();
    	
    	LOG.debug("Add Item [pr = " + pr.getId() + "] : " + this.unitCode + "; " + this.quantity + "; " + this.remarks);
    	
    	PurchaseRequisition prEntity = prService.read(pr.getId());
    	MasterItem miEntity = miService.read(selectedItem.getId());
    	
    	Unit unit = unitService.read(unitCode);
    	
    	if(unit == null) {
    		String customUnit = unitCode;
			//irService.create(prEntity, miEntity, customUnit , quantity, remarks);
    	} else {
    		//irService.create(prEntity, miEntity, unit , quantity, remarks);
    	}
    }
    
    public boolean getRendered() {
    	
    	PurchaseRequisition pr = getSessionPurchaseRequisition();
    	
    	if(pr.getStatus() == Status.OPEN) {
    		return true;
    	}
    	
    	return false;
    }

	/**
	 * @return the unitCode
	 */
	public String getUnitCode() {
		return unitCode;
	}

	/**
	 * @param unitCode the unitCode to set
	 */
	public void setUnitCode(String unitCode) {
		this.unitCode = unitCode;
	}

	/**
	 * @return the quantity
	 */
	public double getQuantity() {
		return quantity;
	}

	/**
	 * @param quantity the quantity to set
	 */
	public void setQuantity(double quantity) {
		this.quantity = quantity;
	}

	/**
	 * @return the remarks
	 */
	public String getRemarks() {
		return remarks;
	}

	/**
	 * @param remarks the remarks to set
	 */
	public void setRemarks(String remarks) {
		this.remarks = remarks;
	}
}
